<?php

namespace Gds\ArticleBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Gds\ArticleBundle\Entity\Famille;
use Gds\ArticleBundle\Form\FamilleType;

class FamilleController extends Controller {

    public function indexAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        $dql = "SELECT c FROM GdsArticleBundle:Famille c WHERE (c.is_supprimer=:is_supprimer and c.libelle LIKE :libelle)";
        $query = $em->createQuery($dql)->setParameters(array('is_supprimer' => false, 'libelle' => '%' . $request->get('libelle') . '%'));
        $paginator = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
                $query, $request->get('page', 1)/* page number */, 20/* limit per page */
        );
        if ($request->get('type')) {
            return $this->render('GdsArticleBundle:Famille:pageList.html.twig', array('pagination' => $pagination));
        }
        return $this->render('GdsArticleBundle:Famille:index.html.twig', array('pagination' => $pagination));
    }

    public function newAction() {
        $famille = new Famille();
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        $famille->setIsSupprimer(false);
        $form = $this->createForm(new FamilleType(), $famille);
        if ($request->isMethod('post')) {
            $form->bind($request);
            if ($form->isValid()) {
                $famille = $form->getData();
                $em->persist($famille);
                $em->flush();
                $red = $request->get('red');
                if ($red == 'n') {
                    $famille = new Famille();
                    $famille->setIsSupprimer(false);
                    $form = $this->createForm(new FamilleType(), $famille);
                    return $this->render('GdsArticleBundle:Famille:new.html.twig', array('form' => $form->createView()));
                } else {
                    $em = $this->getDoctrine()->getEntityManager();
                    $request = $this->getRequest();
                    $dql = "SELECT c FROM GdsArticleBundle:Famille c WHERE (c.is_supprimer=:is_supprimer and c.libelle LIKE :libelle)";
                    $query = $em->createQuery($dql)->setParameters(array('is_supprimer' => false, 'libelle' => '%' . $request->get('libelle')));
                    $paginator = $this->get('knp_paginator');
                    $pagination = $paginator->paginate(
                            $query, $request->get('page', 1)/* page number */, 10/* limit per page */
                    );
                    $pagination->setUsedRoute('gds_famille_article_list');
                    if ($request->get('type')) {
                        return $this->render('GdsArticleBundle:Famille:pageList.html.twig', array('pagination' => $pagination));
                    }
                    return $this->render('GdsArticleBundle:Famille:list.html.twig', array('pagination' => $pagination));
                }
            }
        }
        return $this->render('GdsArticleBundle:Famille:new.html.twig', array('form' => $form->createView()));
    }

    public function listAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        $dql = "SELECT c FROM GdsArticleBundle:Famille c WHERE (c.is_supprimer=:is_supprimer and c.libelle LIKE :libelle)";
        $query = $em->createQuery($dql)->setParameters(array('is_supprimer' => false, 'libelle' => '%' . $request->get('libelle')));
        $paginator = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
                $query, $request->get('page', 1)/* page number */, 10/* limit per page */
        );
        if ($request->get('type')) {
            return $this->render('GdsArticleBundle:Famille:pageList.html.twig', array('pagination' => $pagination));
        }
        return $this->render('GdsArticleBundle:Famille:list.html.twig', array('pagination' => $pagination));
    }

    public function deleteAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        $id = $request->get('id');
        $famille = $em->getRepository('GdsArticleBundle:Famille')->find($id);
        $tab = array();
        $this->deleteFamille($tab, $famille, $em);
        $famille->setIsSupprimer(true);
        $em->persist($famille);
        $em->flush();
        $dql = "SELECT c FROM GdsArticleBundle:Famille c WHERE (c.is_supprimer=:is_supprimer and c.libelle LIKE :libelle)";
        $query = $em->createQuery($dql)->setParameters(array('is_supprimer' => false, 'libelle' => '%' . $request->get('libelle')));
        $paginator = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
                $query, $request->get('page', 1)/* page number */, 10/* limit per page */
        );
        $pagination->setUsedRoute('gds_famille_article_list');
        if ($request->get('type')) {
            return $this->render('GdsArticleBundle:Famille:pageList.html.twig', array('pagination' => $pagination));
        }
        return $this->render('GdsArticleBundle:Famille:list.html.twig', array('pagination' => $pagination));
    }

    public function deleteConfAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        $id = $request->get('id');
        $famille = $em->getRepository('GdsArticleBundle:Famille')->find($id);
        return $this->render('GdsArticleBundle:Famille:deleteConf.html.twig', array('famille' => $famille));
    }

    public function deleteFamille($tab, $famille, $em) {
        foreach ($famille->getArticles() as $article) {
            $article->setIsSipprimer(true);
            $em->persist($article);
            $tab[count($tab)] = $article;
        }
        foreach ($famille->getSousFamilles() as $f) {
            $this->deleteFamille($tab, $f, $em);
            $f->setIsSupprimer(true);
            $em->persist($f);
            $tab[count($tab)] = $f;
        }
    }

    public function editAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        $id = $request->get('id');
        $famille = $em->getRepository('GdsArticleBundle:Famille')->find($id);
        $form = $this->createForm(new FamilleType(), $famille);
        if ($request->isMethod('post')) {
            $form->bind($request);
            if ($form->isValid()) {
                $famille = $form->getData();
                $em->persist($famille);
                $em->flush();
                $em = $this->getDoctrine()->getEntityManager();
                $request = $this->getRequest();
                $dql = "SELECT c FROM GdsArticleBundle:Famille c WHERE (c.is_supprimer=:is_supprimer and c.libelle LIKE :libelle)";
                $query = $em->createQuery($dql)->setParameters(array('is_supprimer' => false, 'libelle' => '%' . $request->get('libelle') . '%'));
                $paginator = $this->get('knp_paginator');
                $pagination = $paginator->paginate(
                        $query, $request->get('page', 1)/* page number */, 10/* limit per page */
                );
                $pagination->setUsedRoute('gds_famille_article_list');
                if ($request->get('type')) {
                    return $this->render('GdsArticleBundle:Famille:pageList.html.twig', array('pagination' => $pagination));
                }
                return $this->render('GdsArticleBundle:Famille:list.html.twig', array('pagination' => $pagination));
            }
        }
        return $this->render('GdsArticleBundle:Famille:edit.html.twig', array('id' => $famille->getId(), 'form' => $form->createView()));
    }

    public function listForArticleAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $familles = $em->getRepository('GdsArticleBundle:Famille')->findAll();
        return $this->render('GdsArticleBundle:Famille:listForArticle.html.twig', array('familles' => $familles));
            
    }

}
